<?php
namespace backend\controllers;

use backend\models\Admin;
use backend\models\AuthGroupAccess;
use common\components\BaseController;
use Yii;
use backend\models\AuthGroup;
use \common\helps\tools;
use \common\helps\code;
use yii\db\Query;


/**
 * AuthGroup controller
 */
class AuthGroupController extends BaseController
{

    public $modelClass = 'backend\models\AuthGroup';
    public function init()
    {
        parent::init();
    }

    /**
     * 获取后台角色列表
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetAuthRoleList(){
        $request =$this->request;
        $model = new AuthGroup();
        $params = $request->queryParams;
        $total_data = $model->getAuthGroupList($params);
        $data = tools::getPageData($total_data,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);

        if(!empty($params['excel_download']) && $params['excel_download']>0){
            $tableHeader = ['id','名称','描述'];
            tools::ToPHPEXECL($tableHeader,$total_data,'后台角色列表');
        }


        return code::success($data);

    }


    /**
     * 删除角色
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionDeleteAuthRole(){
        if($this->request->isPost) {
            $admin_model = new AuthGroup();
            $params = $this->request->bodyParams;
            if(empty($params['role_id'])){
                return code::error('','参数错误');
            }
            $role_id= intval($params['role_id']);
            if($role_id==1){
                return code::error('','超级管理员不允许删除');
            }
            $res = $admin_model->deleteRole($role_id);
            if($res){
                return code::success();
            }else{
                return code::error('','操作失败');
            }

        }

    }

    /**
     * 获取角色权限匹配列表值
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetAuthTreeList(){
        if($this->request->isPost) {
            $admin_model = new AuthGroup();
            $params = $this->request->bodyParams;
            $role_id = empty($params['role_id'])? 0 :intval($params['role_id']);
            if(!($role_id>=0)) return code::error('','参数错误');
            $data=$admin_model->getRoleAuthList($role_id);
            return code::success($data);
        }
    }

    /**
     * 编辑角色
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionEditRoleInfo(){
        if($this->request->isPost) {
            $admin_model = new AuthGroup();
            $params = $this->request->bodyParams;
            if(!empty($params['type']) && $params['type']==2){ //编辑
                $admin_model->scenario='update';
            }else{
                return code::error('','参数错误');
            }
            $admin_model->load($params,'');
            if(!$admin_model->validate()){
                $error = array_values($admin_model->getErrors());
                return code::error('',$error[0][0]);
            }
            $data=$admin_model->editRole($params);
            if(!empty($data['error'])){
                return code::error('',$data['error']);
            }else{
                return code::success();
            }
        }
    }

    /**
     * 新增角色
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionAddRoleInfo(){
        if($this->request->isPost) {
            $admin_model = new AuthGroup();
            $params = $this->request->bodyParams;
            if(!empty($params['type'])&& $params['type']==1){ //新增
                $admin_model->scenario='default';
            }else{
                return code::error('','参数错误');
            }
            $admin_model->load($params,'');
            if(!$admin_model->validate()){
                $error = array_values($admin_model->getErrors());
                return code::error('',$error[0][0]);
            }
            $data=$admin_model->editRole($params);
            if(!empty($data['error'])){
                return code::error('',$data['error']);
            }else{
                return code::success();
            }
        }
    }


    /**
     * 授权角色
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGiveRoleAuth(){
        if($this->request->isPost) {
            $params = $this->request->bodyParams;
            if(empty($params['id'])) return  code::error('','参数错误');
            if(!empty($params['type']) && $params['type']==1){ //获取角色信息
                $admin_model = new Admin();
                $data = $admin_model->getUserListByRole($params['id']);
                return code::success($data);
            }elseif(!empty($params['type']) && $params['type']==2){ // 授权用户
                if(empty($params['user_list_in']) && empty($params['user_list_out'])){
                    return code::success();
                }
                $access_model = new AuthGroupAccess();
                $res = $access_model->editUserRole($params['user_list_in'],$params['user_list_out'],$params['id']);
                if($res){
                    return code::success();
                }else{
                    return  code::error('error');
                }

            }

        }
    }


    /**
     * 获取角色详情
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetAuthRoleInfo(){
        $request =$this->request;
        $params = $request->queryParams;
        $admin_model = new AuthGroup();
        if(empty($params['id'])){  return code::error('','参数错误');}
        $data = (new Query())
            ->select('*')
            ->from('auth_group')
            ->where(['id'=>intval(trim($params['id']))])
            ->one();
        if(!empty($data)){
            $data['tree_list']=$admin_model->getRoleAuthList(intval(trim($params['id'])));
            return code::success($data);
        }else{
            return code::error('','未查询到相关数据');
        }

    }




}
